@extends('admin.base')

@section('content')
    <div class="layui-card">
        <div class="layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form">
                <form id="searchForm">
                    <div class="layui-input-inline">
                        <input type="number" name="mobile" value="{{ $_GET['mobile'] ?? '' }}" id="mobile" placeholder="请输入手机号" class="layui-input">
                    </div>
                    <div class="layui-input-inline">
                        <input type="text" name="username" id="username" value="{{ $_GET['username'] ?? '' }}" placeholder="请输入用户名" class="layui-input">
                    </div>
                    <div class="layui-input-inline">
                        <input type="text" name="register_start" value="{{ $_GET['register_start'] ?? '' }}" id="register_start" class="layui-input" placeholder="注册起始时间">
                    </div>
                    <div class="layui-input-inline">
                        <input type="text" name="register_end" value="{{ $_GET['register_end'] ?? '' }}" id="register_end" class="layui-input" placeholder="注册截止时间">
                    </div>
                    <button type="button" class="layui-btn" id="searchBtn">搜 索</button>
                </form>
            </div>
        </div>
        <div class="layui-card-body">
            <table id="dataTable" lay-filter="dataTable"></table>
            <script type="text/html" id="options">
                <div class="layui-btn-group">
                    @can('user.detail')
{{--                        <a class="layui-btn layui-btn-sm" lay-event="detail">详情</a>--}}
                    @endcan
                </div>
            </script>
        </div>
    </div>
@endsection

@section('script')
    @can('user')
        <script>
            layui.use(['layer', 'table', 'form','laydate'], function () {
                let $ = layui.jquery;
                let layer = layui.layer;
                let form = layui.form;
                let table = layui.table;
                let laydate = layui.laydate;
                laydate.render({elem:'#register_start',type:'datetime'});
                laydate.render({elem:'#register_end',type:'datetime'});
                //用户表格初始化
                let dataTable = table.render({
                    elem: '#dataTable'
                    , height: 'auto'
                    , url: "{{ route('user.data') }}"
                    , page: true //开启分页
                    , where: {
                        mobile: $("#mobile").val(),
                        username: $("#username").val(),
                        register_start: $("#register_start").val(),
                        register_end: $("#register_end").val()
                    }
                    , cols: [[ //表头
                        //{checkbox: true, fixed: true}
                         {field: 'id', title: 'ID', sort: true, width: 80}
                        , {field: 'mobile', title: '手机号',width:120}
                        , {field: 'username', title: '用户名'}
                        , {field: 'nickname', title: '微信昵称'}
                        , {field: 'openid', title: '微信id'}
                        , {field: 'created_at', title: '注册时间',templet:function (d) {
                                return d.created_at.substr(0,16);
                            },width:150}
                        , {field: 'address', title: '地址'}
                        , {field: 'friends_num', title: '好友'}
                        , {title: '操作',fixed: 'right', width: 80, align: 'center', toolbar: '#options'}
                    ]]
                });

                //监听工具条
                table.on('tool(dataTable)', function (obj) { //注：tool是工具条事件名，dataTable是table原始容器的属性 lay-filter="对应的值"
                    let data = obj.data //获得当前行数据
                        , layEvent = obj.event; //获得 lay-event 对应的值
                    if (layEvent === 'detail') {
                        var backUrl = "{{ route('user') }}?";
                        //把查询参数带到返回url上。
                        $.each($("#searchForm").serializeArray(),function (index,field) {
                            backUrl += field.name + '=' + field.value + '&';
                        });
                        //加上分页参数
                        backUrl += 'page=' + dataTable.config.page.curr + '&limit=' + dataTable.config.page.limit;
                        //执行一次urlencode编码，防止在详情页获取backUrl时不全
                        backUrl = encodeURIComponent(backUrl);
                        location.href = '/user/' + data.id + '/detail?backUrl=' + backUrl;
                    }
                });
                //行单击事件,当前行添加额外样式
                table.on('row(dataTable)',function (obj) {
                    $(".layui-table-body tr ").attr({"style":"background:#FFFFFF"});//其他tr恢复原样
                    $(obj.tr.selector).attr({"style":"background:#dbefdb"});//改变当前tr颜色
                })
                //搜索
                $("#searchBtn").on('click',function () {
                    let mobile = $("#mobile").val();
                    let username = $("#username").val();
                    let register_start = $("#register_start").val();
                    let register_end = $("#register_end").val();
                    dataTable.reload({
                        where: {mobile: mobile, username: username, register_start: register_start,register_end: register_end},
                        page: {curr: 1}
                    })
                })
            })
        </script>
    @endcan
@endsection



